package com.example.hospitalbakcends.controller;

import com.example.hospitalbakcends.domain.request.CodeInfoRequest;
import com.example.hospitalbakcends.service.DiagnosisService;
import com.example.hospitalbakcends.utils.QRCode;
import com.google.zxing.WriterException;
import jakarta.annotation.Resource;

import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

@RestController
@CrossOrigin
public class PaymentController {
    @Resource
    public DiagnosisService diagnosisService;

    /**
     * 根据账单id修改是否支付
     * @param
     * @return
     */
    @PostMapping("/setIspayByID")
    public int setIspayByID(@RequestBody Map<String,String> map){
        String id = map.get("id");
        return diagnosisService.setIspayByID(id);
    }

    /**
     * 生成二维码
     * @param content 内容
     * @param name 二维码地址
     * @throws IOException
     * @throws WriterException
     */
    @PostMapping("/createCode")
    public void createCode(@RequestBody CodeInfoRequest codeInfoRequest) throws IOException, WriterException {
        String content = codeInfoRequest.getContent();
        String name = codeInfoRequest.getName();
        QRCode.encode(content,name);
    }
    /**
     * 返回二维码
     * @return 返回一个resource，前端用img标签就可以接收
     */
//    @GetMapping("/getCode")
//    public ResponseEntity<org.springframework.core.io.Resource> getQRCode() {
//        // 二维码文件的路径
//        Path path = Paths.get("123.png");
//        try {
//            org.springframework.core.io.Resource resource = new UrlResource(path.toUri());
//            if (resource.exists() || resource.isReadable()) {
//                return ResponseEntity.ok().body(resource);
//            } else {
//                return ResponseEntity.notFound().build();
//            }
//        } catch (MalformedURLException e) {
//            System.err.println("错误：无图片。");
//            e.printStackTrace();
//            return null;
//        }
//    }
}
